//
//  WJVisitorView.swift
//  新浪微博swift
//
//  Created by WJ-Jason on 2017/5/2.
//  Copyright © 2017年 WJ-Jason. All rights reserved.
//

import UIKit

class WJVisitorView: UIView {
    
    // 注册按钮
    var registerButton : UIButton = UIButton(
        title: "注册",
        fontSize: 16,
        color: UIColor.orange,
        backImageName: "common_button_white_disable")
    // 登录按钮
    var loginButton : UIButton = UIButton(
        title: "登录",
        fontSize: 16,
        color: UIColor.darkGray,
        backImageName: "common_button_white_disable")
    
    
    
    // 访客视图的信息字典 [imageName / message]
    var visitorInfo : [String:String]? {
        didSet {
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    return
            }
            
            tipLabel.text = message
            
            if imageName == "" {
                startAnimation()
                return
            }
            iconView.image = UIImage(named: imageName)
            
            hourseIconView.isHidden = true
            
            maskIconView.isHidden = true
        }
    }
    
    // MARK: - 构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 开始动画
    fileprivate func startAnimation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.isRemovedOnCompletion = false //动画完成不删除,如果IconView被释放,动画会一起销毁
        
        iconView.layer.add(anim, forKey: nil) //将动画添加到图层
    }
    
    // MARK: -私有控件
    
    //    private lazy var ttlabel : UILabel = {
    //        let label = UILabel()
    //
    //        label.text = "123123"
    //        label.font = UIFont.systemFont(ofSize: 15)
    //        return label
    //    }()

    // 懒加载属性只有调用 UIKit 控件的指定构造函数，其他都需要使用类型
    // 图像视图
    internal lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩视图
    internal lazy var maskIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // 小房子
    internal lazy var hourseIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 提示标签
    internal lazy var tipLabel : UILabel = UILabel(
        title: "关注一些人,回这里看看有什么惊喜关注一些人,回这里看看有什么惊喜",
        fontSize: 14,
        color: UIColor.darkGray)
}

// MARK: - 设置界面
extension WJVisitorView {
    func setupUI() -> () {
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(hourseIconView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        tipLabel.textAlignment = .center
        
        //取消 autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //自动布局
        // 1> 图标
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))

        addConstraint(NSLayoutConstraint(item: iconView,
                                        attribute: .centerY,
                                             relatedBy: .equal,
                                             toItem: self,
                                             attribute: .centerY,
                                             multiplier: 1.0,
                                             constant: -60))
        // 2> 小房子
        addConstraint(NSLayoutConstraint(item: hourseIconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: hourseIconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: -60))
        // 3> 消息文字
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 16))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 224))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .height,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 35))
        
        // 4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 16))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton,
                                         attribute: .height,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 35))
        
        // 5> 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: 16))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registerButton,
                                         attribute: .width,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .height,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 35))
        
        // 6> 遮罩视图
        let viewDict = ["maskIconView": maskIconView, "registerButton": registerButton] as [String : Any]
        let metricDict = ["buttonHeight": -35]
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-0-[maskIconView]-0-|",
            options: [],
            metrics: nil,
            views: viewDict))
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-0-[maskIconView]-(buttonHeight)-[registerButton]",
            options: [],
            metrics: metricDict,
            views: viewDict))

        // 背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)

    }
}
